/**
 * related to addShop3.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-12-19
 */
var page = sm("do_Page");
var app = sm("do_App");
var global = deviceone.sm("do_Global");
var nf = sm("do_Notification");
var root = ui("$");
var imgTool = require("helper/picture");
var helperOpen = require("helper/open");
var qqHttp = require("helper/http");
var mallUrl = require("url/mall");
var hashdata = mm("do_HashData");
var linearLayout_main = ui("linearLayout_main");
var checkBox_style1 = ui("checkBox_style1");
var checkBox_style2 = ui("checkBox_style2");
var aLayout_licensePic = ui("aLayout_licensePic");
var do_ALayout_deliveryValue =  ui("do_ALayout_deliveryValue");
var do_TextField_deliveryValue = ui("do_TextField_deliveryValue");
var do_ALayout_freight = ui("do_ALayout_freight");
var do_TextField_freight = ui("do_TextField_freight");
var aLayout_category = ui("aLayout_category");
var label_category = ui("label_category");
var jsonData = {};
var date = sm("do_DateTimePicker");

var dingweiX = null;
var dingweiY = null;
//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
})

/* 头部 */
var headerAout = ui("layoutHead");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
var labelTitle = ui(addheader+".labelTitle");
labelTitle.text='填写资料';
headerLeft.on("touch", function() {
	 goback();
});
//右上角
var headerRight = ui(addheader+".headerRight");
var labelNext = ui(addheader+".labelNext");
labelNext.visible = true;
labelNext.text = "提交审核";

var addressLabelText = ui("addressLabelText");
var txtPhoneText = ui("txtPhoneText");

//andriod返回键
page.on("back", function() {
	 goback();
});

ui("do_ALayout_map").on("touch",function() {
	helperOpen.open("source://view/area/map.ui");
});
page.on("result", function(data) {
	if(null!=data.longitude){//从打开地图定位后回来
		dingweiX = data.longitude;
		dingweiY = data.latitude;
		jsonData.y = dingweiY;
		jsonData.x = dingweiX;
		if(null!=data.address){
			addressLabelText.text=data.address;
		}
	}
	if (data.fromPage && data.fromPage == 'selectShopCategory') {
		label_category.text = data.categoryStr;
		//label_category.fontColor = '000000FF';
		jsonData.categoryId = data.parentId;
		jsonData.categoryStr = data.categoryStr;
	}
});

//返回
function goback() {
	jsonData.phone = txtPhoneText.text;
	jsonData.address = addressLabelText.text;
    app.closePage(jsonData);
}

var styleData = [{"id":1,"checkName":"邻里店","isChecked":false},{"id":2,"checkName":"社区店铺","isChecked":false}];
var shopTypeGrid = ui("shopTypeGrid");
shopTypeGrid.items = styleData;
var sendData;
var sendData1 = [{"id":1,"checkName":"用户自提","isChecked":false},{"id":2,"checkName":"店家配送","isChecked":false},{"id":3,"checkName":"圈圈配送","isChecked":false}];
var sendData2 = [{"id":1,"checkName":"用户自提","isChecked":false},{"id":2,"checkName":"店家配送","isChecked":false}];
var sendDeliveryType = ui("sendDeliveryType");
sendDeliveryType.items = sendData2;
shopTypeGrid.on("touch",function(index){
	var styleText = rodioBox(index,styleData,shopTypeGrid);
	jsonData.style = styleText;
	if (styleText == 2) {
		sendData = sendData1;
		aLayout_licensePic.visible = true;
		sendDeliveryType.items = sendData;
	} else {
		sendData = sendData2
		aLayout_licensePic.visible = false;
		imageView_licensePic.source = '';
		jsonData.licensePic = '';
		sendDeliveryType.items = sendData;
	}
	linearLayout_main.redraw();
});

sendDeliveryType.on("touch",function(index){
	var sendText = checkBox(index,sendData,sendDeliveryType);
	jsonData.sendDeliveryType = sendText;
	if(sendText.indexOf("2") != -1 || sendText.indexOf("3") != -1){//起送金额
		do_ALayout_deliveryValue.visible = true;
		if(sendText.indexOf("2") != -1){
			do_ALayout_freight.visible = true;
		}else{
			do_ALayout_freight.visible = false;
		}
	}else{
		do_ALayout_deliveryValue.visible = false;
	}
	linearLayout_main.redraw();
});

/*var payData = [{"id":1,"checkName":"线下","isChecked":false},{"id":2,"checkName":"线上","isChecked":false}];
var payCheckGrid = ui("payCheckGrid");
payCheckGrid.items = payData;
payCheckGrid.on("touch",function(index){
	var payText = checkBox(index,payData,payCheckGrid);
	jsonData.payType = payText;
});*/

/*多选*/
function checkBox(index,data,id){
	var arr=[];
	data[index].isChecked = !data[index].isChecked;
	id.items = data;
	for(var i in data){
		if(data[i].isChecked == true){
			arr.push(data[i].id);
		}
	}
	return arr.join(";");
}
/*单选*/
function rodioBox(index,data,id){
	var arr=0;
	for(var i in data){
		if(i==index){
			data[i].isChecked = true;
			arr = data[i].id;
		}
		else{
			data[i].isChecked = false;
		}
	}
	id.items = data;
	return arr;
}

var hasUploadImg1 = false;
var hasUploadImg2 = false;
headerRight.on("touch",'' ,300,function(data, e) {
	page.hideKeyboard();
	if (jsonData.logo == null || jsonData.logo == '') {
		nf.toast("亲，请上传店铺LOGO");
		return;
	}
	if(txtPhoneText.text=='' || txtPhoneText.text==null){
		nf.toast("亲，请填写联系方式。");
		return;
	}
	if (dingweiX=='' || dingweiX == null) {
		nf.toast("亲，请标注地图。");
		return;
	}
	if(addressLabelText.text=='' || addressLabelText.text==null){
		nf.toast("亲，请填写详细地址。");
		return;
	}
	if (jsonData.busStart == null || jsonData.busEnd == null) {
		nf.toast("亲，请选择营业时间");
		return;
	}
	if (jsonData.style == null) {
		nf.toast("亲，请选择店铺类型");
		return;
	}
	jsonData.phone = txtPhoneText.text;
	jsonData.address = addressLabelText.text;
	jsonData.payType = '1;2';
	if(do_TextField_deliveryValue.text != ''){
		jsonData.deliveryValue = do_TextField_deliveryValue.text;
	}
	if(do_TextField_freight.text != ''){
		jsonData.freight = do_TextField_freight.text;
	}
	//nf.alert(JSON.stringify(jsonData));
	var imgFiles = [];
	imgFiles.push(jsonData.logo);
	if (jsonData.licensePic != '') {
		imgFiles.push(jsonData.licensePic);
	}else{
		hasUploadImg2 = true;
	}
	if (hasUploadImg1 && hasUploadImg2) {
		qqHttp.post(mallUrl.url.shop.doAdd,jsonData,function(data){
			nf.toast("提交成功,请等待审核.");
			app.closePage(null,null,3);
			helperOpen.open("source://view/qshop/manage/shop/myShop.ui");
		});
	} else if (hasUploadImg2) {
		qqHttp.upload(jsonData.logo,"mall/shop",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			if(string4imgs==""){
				nf.toast("请重新上传LOGO");
				return;
			}
			hasUploadImg1 = true;
			jsonData.logo = string4imgs;
			qqHttp.post(mallUrl.url.shop.doAdd,jsonData,function(data){
				nf.toast("提交成功,请等待审核.");
				app.closePage(null,null,3);
				helperOpen.open("source://view/qshop/manage/shop/myShop.ui");
			});
		});
	} else if (hasUploadImg1) {
		qqHttp.upload(jsonData.licensePic,"mall/shop",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			if(string4imgs==""){
				nf.toast("请重新上传营业执照");
				return;
			}
			hasUploadImg2 = true;
			jsonData.licensePic = string4imgs;
			qqHttp.post(mallUrl.url.shop.doAdd,jsonData,function(data){
				nf.toast("提交成功,请等待审核.");
				app.closePage(null,null,3);
				helperOpen.open("source://view/qshop/manage/shop/myShop.ui");
			});
		});
	} else {
		qqHttp.form(imgFiles,"mall/shop",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			if(string4imgs==""){
				nf.toast("请重新上传图片");
				return;
			}
			hasUploadImg1 = true;
			hasUploadImg2 = true;
			var upImg = string4imgs.split(';');
			if (upImg.length == 1) {
				jsonData.logo = upImg[0];
			} else if (upImg.length == 2) {
				jsonData.logo = upImg[0];
				jsonData.licensePic = upImg[1];
			}
			qqHttp.post(mallUrl.url.shop.doAdd,jsonData,function(data){
				nf.toast("提交成功,请等待审核.");
				app.closePage(null,null,3);
				helperOpen.open("source://view/qshop/manage/shop/myShop.ui");
			});
		});
	}
});
aLayout_licensePic.on("touch", function(data, e) {
	imgTool.imgCut(selectImgBack);
});
var imageView_licensePic = ui("imageView_licensePic");
function selectImgBack(imgList){
	imageView_licensePic.source=imgList[0];
	jsonData.licensePic =imgList[0];
}

//定义时间
var myDate=new Date();
var date0 = myDate.getTime();
var startTime = ui("startTime");
var endTime = ui("endTime");
//定义最小时间
myDate.setHours(0,0,0,0);
var dateMin = myDate.getTime();
//定义最大时间
myDate.setHours(23,59,59,999);
var dateMax = myDate.getTime();
//选择时间方式，最大最小日期为默认，预设时间为当前时间，不显示按钮
ui("startTimeBox").on("touch","",300,function(){
	page.hideKeyboard();
    date.show({type:2, data:date0, maxDate:dateMax,minDate:dateMin, title:"时间选择", buttons:["选择","不选择"]}, function(data, e){
    	if (data.flag == 0) {
    		var sTime=parseInt(data.time,10);
        	startTime.text = timeFormat(sTime);
        	jsonData.busStart = startTime.text;
    	}
    });
})

ui("endTimeBox").on("touch","",300,function(){
	page.hideKeyboard();
    date.show({type:2, data:date0, maxDate:dateMax,minDate:dateMin, title:"时间选择", buttons:["选择","不选择"]}, function(data, e){
    	if (data.flag == 0) {
    		var eTime=parseInt(data.time,10);
        	endTime.text = timeFormat(eTime);
        	jsonData.busEnd = endTime.text;
    	}
    });
})

function timeFormat(time){
	var svDate = new Date(time);//要格式化的时间
	var minutes=0;
	if(svDate.getMinutes()<10){
		minutes="0"+svDate.getMinutes();
	}else{
		minutes=svDate.getMinutes();
	}
	var hours=0;
	if(svDate.getHours()<10){
		hours="0"+svDate.getHours();
	}else{
		hours=svDate.getHours();
	}
	return hours+':'+minutes;
}

aLayout_category.on("touch", function(data, e) {
	page.hideKeyboard();
	if (jsonData.style == null) {
		nf.toast("请选择店铺类型");
		return;
	}
	global.setMemory("shopCategoryCount", 0);
	helperOpen.open("source://view/qshop/manage/shop/selectCategory.ui",{parentId:0,categoryStr:'',type:jsonData.style});
});

page.on("loaded", function(){
	jsonData = page.getData();
	jsonData.communityId = "";
	jsonData.communityAddrId = "";
	if (jsonData.phone != null && jsonData.phone != '') {
		txtPhoneText.text = jsonData.phone;
	}
	if (jsonData.busStart != null) {
		startTime.text = jsonData.busStart;
	}
	if (jsonData.busEnd != null) {
		endTime.text = jsonData.busEnd;
	}
	if (jsonData.sendDeliveryType != null && jsonData.sendDeliveryType !='') {
		var temp = jsonData.sendDeliveryType.split(';');
		for (var i=0; i < temp.length; i++) {
			if (temp[i] == '1') {
				sendData[0].isChecked = true;
			} else if (temp[i] == '2') {
				sendData[1].isChecked = true;
			} else if (temp[i] == '3') {
				sendData[2].isChecked = true;
			}
		}
		sendDeliveryType.items = sendData;
	}
	if (jsonData.categoryId ==null || jsonData.categoryId=="") {
		label_category.text = jsonData.categoryStr;
	}
/*	if (jsonData.payType != null && jsonData.payType != '') {
		var temp = jsonData.payType.split(';');
		for (var i=0; i < temp.length; i++) {
			if (temp[i] == '1') {
				payData[0].isChecked = true;
			} else if (temp[i] == '2') {
				payData[1].isChecked = true;
			}
		}
		payCheckGrid.items = payData;
	}*/
});